<?php
include('../../../includes/classes/core.php');



$start = $_REQUEST['start'];
$end   = $_REQUEST['end'];
$action = $_REQUEST['act'];
$name = $_REQUEST['name'];
$error = '';
$data = '';

function GetSum($start, $end){
$result = mysql_fetch_assoc(mysql_query("SELECT 	IFNULL(ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2),0) AS `sum`
										   FROM     sale_transaction
										   JOIN     sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
										  WHERE     DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.status=0"));
return $result['sum'];
}

function GetProductSum($name, $start, $end){
	$result = mysql_fetch_assoc(mysql_query("SELECT 	IFNULL(ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2),0) AS `sum`
											   FROM     sale_transaction
											   JOIN     sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
											   JOIN     production ON production.id = sale_transaction_detail.production_id 
											  WHERE     production.`name` = '$name' AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end' AND sale_transaction.`status` =0"));
	return $result['sum'];	
}


switch ($action) {

	case 'get_sale_sum' :
		
		$data	= array('page' => GetSum($start, $end));
		break;
	case 'get_products_sum' :
		

		$page = GetProductSum($name, $start, $end);
		
		$data	= array('page' => $page);
		break;

	default :
		echo "Action is null!";
		break;

}

$data['error'] = $error;
echo json_encode($data);

?>